package com.campus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.constant.CourseStatus;
import com.campus.dao.ManagerClassCourseMapper;
import com.campus.entry.Course;
import com.campus.entry.ManagerClass;
import com.campus.exception.BusinessException;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.CourseService;
import com.campus.service.ManagerTeacherService;
import com.campus.util.GetUserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ManagerTeacherService managerTeacherService;
    @Autowired
    private ManagerClassCourseMapper managerClassMapper;

    /**
     * 保存课程
     * @param course
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PostMapping("/save")
    public ResponseResult<Object> createCourse(@RequestBody Course course){
        return getObjectResponseResult(course);
    }

    /**
     * 教师城建课程
     * @param course
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PostMapping("/teacher/save")
    public ResponseResult<Object> teacherSaveCourse( Course course){
        course.setTeacherId(managerTeacherService.getTeacherIdByLoginId(GetUserIdUtil.getUserId()));
        return getObjectResponseResult(course);
    }

    private ResponseResult<Object> getObjectResponseResult(Course course) {
        if (course.getCourseName()==null||course.getClassId()==null){
            throw new BusinessException(ResultCode.ERROR);
        }
        LambdaQueryWrapper<ManagerClass> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerClass::getId,course.getClassId());
        ManagerClass managerClass = managerClassMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(managerClass)){
            throw new BusinessException(201,"课程不存在");
        }
        Integer activityId=managerClass.getActivityId();
        if (Objects.isNull(activityId)){
            throw new BusinessException(201,"课程不存在");
        }
        course.setActivityId(activityId);
        course.setTeacherId(managerTeacherService.getTeacherIdByLoginId(GetUserIdUtil.getUserId()));
        course.setCreatedTime(LocalDateTime.now());
        course.setStatus(CourseStatus.CREATE);
        courseService.createCourse(course);
        return new  ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 教师模糊查询课程
     * @param status
     * @param courseName
     * @return {@link ResponseResult }<{@link List }<{@link Course }>>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @GetMapping("/listByTeacherId")
    public ResponseResult<List<Course>> getCourseByTeacherId(@RequestParam String status, @RequestParam String courseName){
        Integer id= GetUserIdUtil.getUserId();
        Integer teacherIdByLoginId = managerTeacherService.getTeacherIdByLoginId(id);
        List<Course> courseByTeacherId = courseService.getCourseByTeacherId(teacherIdByLoginId,status,courseName);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,courseByTeacherId);
    }

    /**
     * 打开课程
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/open/{id}")
    public ResponseResult<Object> openCourse(@PathVariable Integer id){
            courseService.updateStatus(id, CourseStatus.OPEN);
            return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 关闭课程
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/close/{id}")
    public ResponseResult<Object> closeCourse(@PathVariable Integer id){
        courseService.updateStatus(id, CourseStatus.CLOSE);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据Id查询课程
     * @param id
     * @return {@link ResponseResult }<{@link Course }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @GetMapping("/{id}")
    public ResponseResult<Course> getCourseById(@PathVariable Integer id){
        Course course=courseService.getCourseById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,course);
    }

    /**
     * 删除课程
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Object> removeCourseById(@PathVariable Integer id){
        courseService.deleteCourseById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 更新课程信息
     * @param course
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @PutMapping("/update")
    public ResponseResult<Object> updateCourse(@RequestBody Course course){
        if (course.getId()==null||course.getCourseName()==null||course.getClassId()==null){
            throw new BusinessException(ResultCode.ERROR);
        }
        LambdaQueryWrapper<ManagerClass> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerClass::getId,course.getClassId());
        ManagerClass managerClass = managerClassMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(managerClass)){
            throw new BusinessException(201,"课程不存在");
        }
        Integer activityId=managerClass.getActivityId();
        if (Objects.isNull(activityId)){
            throw new BusinessException(201,"课程不存在");
        }
        course.setActivityId(activityId);
        courseService.updateCourse(course);
        return new  ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }
 }
